/******************* 公共变量 *******************/
var lazy,
	longitude,
	latitude,
	sortIndex,
	sex,
	sexIndex;

/******************* 数据载入 *******************/
$.ajax({
	url: checkNews,
	type: 'post',
	success: function(data) {
		console.log(data);

		common.hintNewMsg(data);
	}
});

/******************* 初始化微信 *******************/
wx.ready(function () {
	wx.checkJsApi({  
		jsApiList : ['getLocation', 'uploadImage','previewImage','chooseImage','scanQRCode','startRecord','stopRecord','onVoiceRecordEnd','playVoice','onVoicePlayEnd','onMenuShareAppMessage'],  
		success : function(res) {  
		},
		complete: function (data) {
			if (data.checkResult.getLocation == 'no') {
				useGps(true);
			} else {
				useGps();
			}
		}
	});
});

function useGps(useBaidu) {
	var bdMap = core.bdMap({
		gpsDetail: function (data) {		// 根据经纬度获取地址信息
			console.log(data);
			var lng = data.result.location.lng,
				lat = data.result.location.lat;
			
			longitude = data.result.location.lng;
			latitude = data.result.location.lat;
			lazy = ajax.lazy({
				device: 'mobile',
				// refreshHeight: 4 * rem,
				con: ['html body .wrap .playmate-index .playmate-box'],
				win: ['html body .wrap'],
				winHeight: [winHeight],
				data: [function (num) {
					return {
						page: num,
						lng: lng,
						lat: lat
					}
				}],
				// pageLoading: true,
				// lazyLoading: true,
				refresh: false,
				imgloading: true,
				noData: 'body .no-data',
				type: 'post',
				url: [function (num) {
					return userList;
				}],
				prop: 'list',
				cycle: [{
					list: {
						eleFun: function (data) {
							// console.log(data)
							return '<dl>' + 
								'<dd class="detail">' + 
									'<span>' + data.username + '</span>' + 
									'<strong class="' + ((data.sex == 1) ? 'boy' : 'girl') + '">' + data.age + '</strong>' + 
								'</dd>' + 
								'<dt>' + 
									'<a href="' + userDetailUrl + data.id + '&from=1" title="">' + 
										'<img src="' + data.photo_id + '" alt="" title="">' + 
										// ((data.is_verify == 0) ? '<span class="not-verify">未认证</span>' : '<span class="is-verify">已认证</span>') + 
									'</a>' + 
								'</dt>' + 
								'<dd class="position">' + 
									'<span>' + data.add_time + '</span>' + 
									'<strong>' + data.km + 'km</strong>' + 
									'<div user-id="' + data.id + '" class="' + ((data.is_friend == 1) ? 'focused' : '') + '">' + 
										'<em>' + ((data.is_friend == 1) ? '已关注' : '+关注') + '</em>' + 
									'</div>' + 
								'</dd>' + 
							'</dl>';
						}
					}
				}]
			});
		},
		// bdGps: function (lng, lat) {
		// }
	}, useBaidu);

	/******************* 关注功能 *******************/
	$('html body .wrap .playmate-index .playmate-box').on('tap', 'dl dd.position div', function () {
		var $this = $(this);
		$.ajax({
			url: userUrl,
			type: 'post',
			success: function (data) {
				console.log(data);
				if (data.list.is_verify == 1) {
					if (!$this.hasClass('focused')) {
						$.ajax({
							url: focusUrl,
							type: 'post',
							data: {fo_user_id: $this.attr('user-id')},
							success: function (data) {
								console.log(data)

								if (data.result == 0) {
									core.layerHint(data.message);
								} else if (data.result == 1) {
									core.layerHint('已成功关注，TA将可直接报名您发布的活动');
									$this.addClass('focused').html('<em>已关注</em>');
								}
							}
						});
					} else {
						$.ajax({
							url: noFocusUrl,
							type: 'post',
							data: {fo_user_id: $this.attr('user-id')},
							success: function (data) {
								console.log(data)

								if (data.result == 0) {
									core.layerHint(data.message);
								} else if (data.result == 1) {
									core.layerHint(data.message);
									$this.removeClass('focused').html('<em>+关注</em>');
								}
							}
						});
					}
				} else {
					if (data.list.jump_status == 4) {
						core.href(userIdentifyUrl + '?user_id=' + data.list.id);
					} else if (data.list.jump_status == 5) {
						core.href(userHeaderUrl + '?user_id=' + data.list.id);
					} else {
						core.href(userCompleteUrl);
					}
				}
			}
		});
	});

	/******************* 搜索弹窗 *******************/
	$('html body .header-title .search').on('tap', function () {
		layer.open({
			content: '<h2>请选择筛选条件</h2>\
			<strong></strong>\
			<div class="sort">\
				<h3>排序</h3>\
				<div class="con">\
					<span><i></i>最新活跃</span>\
					<span><i></i>离我最近</span>\
				</div>\
			</div>\
			<div class="sex">\
				<h3>性别</h3>\
				<div class="con">\
					<span sex="0"><i></i>不限</span>\
					<span sex="2"><i></i>女</span>\
					<span sex="1"><i></i>男</span>\
				</div>\
			</div>\
			<div class="btn">\
				<span>确定</span>\
				<em>重置条件</em>\
			</div>',
			shadeClose: false,
			className: 'index-search playmate-search',
			success: function ($ele) {
				// 关闭按钮
				$('body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search strong').on('tap', function () {
					layer.closeAll();
				});
				// 排序选择
				console.log('sortIndex = ' + sortIndex);
				var sort = core.tabSwitch({
					tit: 'body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search .sort .con span',
					event: 'tap',
					active: sortIndex
				});
				// 性别选择
				console.log('sexIndex = ' + sexIndex);
				var sex = core.tabSwitch({
					tit: 'body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search .sex .con span',
					event: 'tap',
					active: sexIndex
				});
				// 点击重置按钮
				$('body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search .btn em').on('tap', function () {
					sort.change(0);
					classify.change(0);
					sex.change(0);
					theme.change(0);
				});

				// 确定按钮
				$('body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search .btn span').on('tap', function () {
					sortIndex = $('body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search .sort .con .active').index();
					sexIndex = $('body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search .sex .con .active').index();
					sex = $('body .layui-m-layer .layui-m-layermain .layui-m-layersection .index-search .sex .con .active').attr('sex');
					lazy.tab(0, {
						url: function(num) {
							return userList;
						},
						data: function (num) {
							return {
								page: num,
								type: sortIndex + 1,
								sex: sex,
								lng: longitude,
								lat: latitude
							}
						}
					});
					layer.closeAll();
				});
			}
		});
	});

	$('body').on('tap', '.layui-m-layershade', function () {
		layer.close($('.layui-m-layer').attr('index'));
	});
}
